package com.agger.harmonyosthemeseditor.intelligent;

import ohos.app.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DatabaseManager
{
    private static String databaseStoragePath = null;
    private static String DatabaseStringPath = null;
    private static String DatabaseIntPath = null;

    public static void Init(Context context)
    {
        databaseStoragePath = context.getFilesDir() + "/DatabaseStorage";
        File DatabaseStorage = new File(databaseStoragePath);
        if(!DatabaseStorage.exists())DatabaseStorage.mkdirs();
        DatabaseStringPath = databaseStoragePath +"/String";
        File DatabaseString = new File(DatabaseStringPath);
        if(!DatabaseString.exists())DatabaseString.mkdirs();
        DatabaseIntPath = databaseStoragePath +"/Int";
        File DatabaseInt = new File(DatabaseIntPath);
        if(!DatabaseInt.exists())DatabaseInt.mkdirs();
    }

    public static void Init(String storagePath)
    {
        databaseStoragePath = storagePath + "/DatabaseStorage";
        File DatabaseStorage = new File(databaseStoragePath);
        if(!DatabaseStorage.exists())DatabaseStorage.mkdirs();
        DatabaseStringPath = databaseStoragePath +"/String";
        File DatabaseString = new File(DatabaseStringPath);
        if(!DatabaseString.exists())DatabaseString.mkdirs();
        DatabaseIntPath = databaseStoragePath +"/Int";
        File DatabaseInt = new File(DatabaseIntPath);
        if(!DatabaseInt.exists())DatabaseInt.mkdirs();
    }

    public static void setString(String key,String value)
    {
        File KeyStringFile = new File(DatabaseStringPath+"/"+key);
        if(KeyStringFile.exists()) KeyStringFile.delete();
        File BuildKeyStringFile = new File(DatabaseStringPath+"/"+key);
        FileOutputStream fileOutputStream = null;
        try
        {
            fileOutputStream = new FileOutputStream(BuildKeyStringFile);
            BuildKeyStringFile.createNewFile();
            byte[] valueInBytes = value.getBytes();
            fileOutputStream.write(valueInBytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        }catch(IOException ex){ex.printStackTrace();}
        finally
        {
            try
            {
                if (fileOutputStream != null)
                {
                    fileOutputStream.close();
                }
            } catch(IOException e){e.printStackTrace();}
        }
    }

    public static String getString(String key)
    {
        File KeyStringFile = new File(DatabaseStringPath+"/"+key);
        if(KeyStringFile.exists())
        {
            try
            {
                FileInputStream in = new FileInputStream(KeyStringFile);
                byte[] b = new byte[(int) KeyStringFile.length()];
                in.read(b);
                String result = new String(b);
                in.close();
                return result;
            }catch(IOException ex)
            {
                return null;
            }
        }else
        {
            return null;
        }
    }

    public static String getString(String key,String Default)
    {
        File KeyStringFile = new File(DatabaseStringPath+"/"+key);
        if(KeyStringFile.exists())
        {
            try
            {
                FileInputStream in = new FileInputStream(KeyStringFile);
                byte[] b = new byte[(int) KeyStringFile.length()];
                in.read(b);
                String result = new String(b);
                in.close();
                return result;
            }catch(IOException ex) {return Default;}
        }else
        {
            return Default;
        }
    }

    public static void setInt(String key,int value)
    {
        File KeyIntFile = new File(DatabaseIntPath+"/"+key);
        if(KeyIntFile.exists()) KeyIntFile.delete();
        File BuildKeyIntFile = new File(DatabaseIntPath+"/"+key);
        FileOutputStream fileOutputStream = null;
        try
        {
            fileOutputStream = new FileOutputStream(BuildKeyIntFile);
            BuildKeyIntFile.createNewFile();
            byte[] valueInBytes = Integer.toString(value).getBytes();
            fileOutputStream.write(valueInBytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        }catch(IOException ex){ex.printStackTrace();}
        finally
        {
            try
            {
                if (fileOutputStream != null)
                {
                    fileOutputStream.close();
                }
            } catch(IOException e){e.printStackTrace();}
        }
    }

    public static int getInt(String key)
    {
        File KeyIntFile = new File(DatabaseIntPath+"/"+key);
        if(KeyIntFile.exists())
        {
            try
            {
                FileInputStream in = new FileInputStream(KeyIntFile);
                byte[] b = new byte[(int) KeyIntFile.length()];
                in.read(b);
                String temp = new String(b);
                in.close();
                return Integer.valueOf(temp);
            }catch(IOException ex) {return -1;}
        }else
        {
            return -1;
        }
    }

    public static int getInt(String key,int Default)
    {
        File KeyIntFile = new File(DatabaseIntPath+"/"+key);
        if(KeyIntFile.exists())
        {
            try
            {
                FileInputStream in = new FileInputStream(KeyIntFile);
                byte[] b = new byte[(int) KeyIntFile.length()];
                in.read(b);
                String temp = new String(b);
                in.close();
                return Integer.valueOf(temp);
            }catch(IOException ex) {return Default;}
        }else
        {
            return Default;
        }
    }

    public static void ClearAll()
    {
        FileManager.deleteDirectory(databaseStoragePath,true,false);
    }
}
